package com.goodedeeds.webapp.controller;

import com.goodedeeds.domain.resource.Project;
import com.goodedeeds.domain.resource.School;
import com.youtills.core.admin.ILookup;
import com.youtills.core.admin.LookupType;
import com.youtills.core.admin.db.dao.AdminDao;
import com.youtills.core.lang.StringUtil;
import com.youtills.domain.contact.Address;
import com.youtills.resource.ResourceManager;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: rchiluve
 * Date: 2/23/14
 * Time: 8:15 PM
 * To change this template use File | Settings | File Templates.
 */
@UrlBinding(SchoolAction.URL)
public class SchoolAction extends BaseDashboardAction {

    public static final String URL = "/school.action";

    private static final String VIEW = JSP_ROOT + "/school-view.jsp";
    private static final String EDIT_VIEW = JSP_ROOT + "/school-edit.jsp";
    private static final String LIST_VIEW = JSP_ROOT + "/school-list.jsp";

    private Long schoolId;

    private School school;
    private List<Project> projects;

    private String dashboardContent;

    @Override
    public String getDashboardContent() {
        return dashboardContent;
    }

    @Override
    public DashboardTab getCurrentTab() {
        return DashboardTab.NONE;
    }

    public School getSchool() {
        if(school == null){
//            school = DomainFactory.getSchoolDao().getSchool(null, getSchoolId());
            if(getSchoolId() < 1) {
                school = ResourceManager.instance.newResource(getActionContext(), School.class);
            } else {
                school = ResourceManager.instance.getResource(getActionContext(), School.class, getSchoolId());
            }
        }
        return school;
    }

    public Address getSchoolAddress() {
        School school = getSchool();
        if(school != null) {
            return school.getAddress(getActionContext());
        }
        return null;
    }

    public List<Project> getProjects() {
        if(projects == null) {
            projects = getSchool().getProjects(getActionContext());
        }
        return projects;
    }

    public Long getSchoolId() {
        if(schoolId == null){
            schoolId = StringUtil.getNumber(getRequestParameter("schoolId"), 0).longValue();
        }
        return schoolId;
    }

    public void setSchoolId(Long schoolId) {
        this.schoolId = schoolId;
    }

    public Resolution save(){
//        DomainFactory.getSchoolDao().saveSchool(null, school);
        ResourceManager.instance.saveResource(getActionContext(), school);
        schoolId = school.getSchoolId();
        RedirectResolution res = new RedirectResolution(SchoolAction.class, "view");
        res.addParameter("schoolId", schoolId);
        return res;
    }

    private List<School> schools;
    public List<School> getSchools(){
        if(schools == null){
            schools = ResourceManager.instance.getResources(getActionContext(), School.class, null);
        }
        return schools;
    }


    @DefaultHandler
    public Resolution list(){
        dashboardContent = LIST_VIEW;
        return showPage();
    }

    public Resolution view(){
        dashboardContent = VIEW;
        return showPage();
    }

    public Resolution edit() {
        dashboardContent = EDIT_VIEW;
        return showPage();
    }

    private List<ILookup> schoolStatuses;
    public List<ILookup> getSchoolStatuses(){
        if(schoolStatuses == null){
            schoolStatuses = AdminDao.getInstance().getLookups(LookupType.getByType(5L));
        }

        return schoolStatuses;
    }
}
